<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板类型" prop="templateType">
        <el-select v-model="queryParams.templateType" placeholder="选择模板类型查询" clearable @change="handleQuery">
          <el-option
            label="平台模板"
            :value="0"
          />
          <el-option
            label="单位模板"
            :value="1"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb-8" v-hasPermi="['saas:form:add']">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >创建模板</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-row v-for="(item,index) in tableList" :key="index" class="forBox">
      <el-col :span="8">
        <div class="disFlxAC py-10">
          <img
            :src="item.templateIcon"
            style="width:80px;height:80px;"
          />
          <div class="px-10">
            <div>{{item.templateName}}</div>
            <div class="color9 pt-5">{{item.templateDesc}}</div>
          </div>
        </div>
      </el-col>
      <el-col :span="4">
        <div style="height:100px;" class="disFlxCC">
          {{item.templateType === 0 ? '平台模板' : '单位模板'}}
        </div>
      </el-col>
      <el-col :span="4">
        <div style="height:100px;" class="disFlxCC">
          {{item.openAuth === 0 ? '全部可见' : '指定可见'}}
        </div>
      </el-col>
      <el-col :span="4">
        <div style="height:100px;" class="disFlxCC">
          <el-switch v-model="item.enableState" :active-value="1" active-text="启用" :inactive-value="0" inactive-text="停用" @change="upStatus(item)"></el-switch>
        </div>
      </el-col>
      <el-col :span="4">
        <div style="height:100px;" class="disFlxCC">
          <div>
            <el-button type="primary" size="mini" @click="handleUpdata(item)">修改</el-button>
            <!-- <el-button type="primary" size="mini" @click="">添加</el-button> -->
          </div>
        </div>
      </el-col>
    </el-row>
    <pagination
      v-show="total > 0 && total > queryParams.pageSize"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getTemplateList, updateTemplateStatus } from "@/api/customForm/customForm";

export default {
  name: "PlatformTEM",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: "",
        templateType: null
      },
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      getTemplateList(this.queryParams).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 跳转新增模板
    handleAdd() {
      this.$router.push({ path: `/form/build/index?type=1` });
    },
    // 修改模板
    handleUpdata(item){
      let url = `/form/build/index?type=1`
      url += item.templateId ? `&templateId=${item.templateId}` : ""
      url += item.formId ? `&formId=${item.formId}` : ""
      this.$router.push({ path: url });
    },
    // 更新狀態
    upStatus(row){
      console.log();
      let params = {
        templateId: row.templateId,
        enableState: row.enableState
      }
      updateTemplateStatus(params).then(res=>{
        this.$message.success('修改状态成功')
      }).catch((err) => {
        row.enableState = (row.enableState === 0 ? 1 : 0)
        this.$message.error('修改状态失败')
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.forBox{
  font-size: 14px;
  cursor: pointer;
}
.forBox + .forBox{
  border-top: solid 1px #e5e5e5;
}
</style>
